package org.shj.netty.chartper12;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;

public class ChineseProverbServer {
	
	public void run(final int port) throws Exception{
		//配置服务端的NIO线程组
		EventLoopGroup bossGroup = new NioEventLoopGroup();
		try{
			Bootstrap b = new Bootstrap();
			b.group(bossGroup)
			 .channel(NioDatagramChannel.class)
			 .option(ChannelOption.SO_BROADCAST, true)
			 .handler(new ChineseProverbServerHandler()); //UDP 不存在客户端和服务端的实际连接，因此不需要为连接(ChannelPipeline)设置 handler
			
			//绑定端口， 同步等待成功
			Channel ch = b.bind(port).sync().channel();
			
			ch.closeFuture().await();
		}finally{
			bossGroup.shutdownGracefully();
		}
	}
		
	public static void main(String[] args) throws Exception{
		int port = 8080;
		if(args != null && args.length > 0){
			try{
				port = Integer.valueOf(args[0]);
			}catch(NumberFormatException e){
				
			}
		}
		
		new ChineseProverbServer().run(port);
	}
}
